package com.ssd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssd.dto.TaskDto;
import com.ssd.entity.Task;
import com.ssd.entity.TaskType;
import com.ssd.mapper.TaskMapper;
import com.ssd.mapper.TaskTypeMapper;
import com.ssd.service.TaskService;
import com.ssd.utils.IdUtils;
import com.ssd.viewmodel.Pager;
import com.ssd.vo.CancelTaskVO;
import com.ssd.vo.TaskVO;
import com.ssd.vo.UpdateStatusVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 工单Service业务层处理
 *
 * @author itheima
 * @date 2024-07-24
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
    @Autowired
    private TaskTypeMapper taskTypeMapper;

    @Override
    public Long createTask(TaskDto taskDto) {
        //构建存储对象
        Task task = new Task();
        long code = IdUtils.getSnowflake().nextId();
        task.setTaskCode(code+"");
        task.setCreateType(1);//默认为手动创建
        task.setDesc(taskDto.getDesc());
        task.setInnerCode(taskDto.getInnerCode());
        task.setProductTypeId(taskDto.getProductTypeId());
        task.setTaskName(taskDto.getTaskName());
        task.setTaskStatus(2);//创建为进心中的工单
        task.setCreateTime(LocalDateTime.now());
        task.setUserId(1L);
        int insert = baseMapper.insert(task);
        //判断是否为补货工单
        return task.getTaskId();//返回工单id
    }

    @Override
    public Pager<TaskVO> search(List<Integer> productTypeIdList, Integer productTypeId, Integer taskStatus, String taskCode, Integer pageIndex, Integer pageSize) {
        //分页对象
        Page<Task> page = new Page<>(pageIndex, pageSize);
        //构建查询条件
        QueryWrapper<Task> qw = Wrappers.query();
        qw.in("product_type_id", productTypeIdList);
        if (productTypeId != null) {
            qw.eq("product_type_id", productTypeId);
        }
        if (taskStatus != null) {
            qw.eq("task_status", taskStatus);
        }
        if (!"".equals(taskCode) && taskCode != null) {
            qw.like("task_code", taskCode);
        }
        //查询列表
        Page<Task> taskPage = this.page(page, qw);
        //使用VO对象返回数据
        Pager<TaskVO> taskVOPager = new Pager<>();
        List<TaskVO> taskVOList = new ArrayList<>();
        for (Task task : taskPage.getRecords()) {
            TaskVO taskVO = new TaskVO();
            taskVO.setAssignorId(task.getAssignorId());
            taskVO.setCreateType(task.getCreateType());
            taskVO.setDesc(task.getDesc());
            taskVO.setInnerCode(task.getInnerCode());
            taskVO.setProductTypeId(task.getProductTypeId());
            taskVO.setRegionId(task.getRegionId());
            taskVO.setTaskCode(task.getTaskCode());
            taskVO.setTaskId(task.getTaskId());
            taskVO.setTaskName(task.getTaskName());
            taskVO.setTaskStatus(task.getTaskStatus());
            taskVO.setUserId(task.getUserId());
            taskVO.setUserName(task.getUserName());
            taskVO.setCreateTime(task.getCreateTime());
            taskVO.setUpdateTime(task.getUpdateTime());
            //查询工单类型
            TaskType taskType = taskTypeMapper.selectById(task.getProductTypeId());
            taskVO.setTaskType(taskType);
            taskVOList.add(taskVO);
        }
        //转换page对象
        taskVOPager.setCurrentPageRecords(taskVOList);
        taskVOPager.setTotalPage(taskPage.getPages());
        taskVOPager.setTotalCount(taskPage.getTotal());
        taskVOPager.setPageIndex(taskPage.getCurrent());
        taskVOPager.setPageSize(taskPage.getSize());
        return taskVOPager;
    }

    @Override
    public Task getByTaskId(Long taskId) {
        return baseMapper.selectById(taskId);
    }

    @Override
    public Integer getTotalTask(LocalDate start, LocalDate end, Integer status) {
        return baseMapper.getTotalTask(start, end, status);
    }

    @Override
    public boolean cancel(CancelTaskVO cancelTaskVO){
        //修改工单状态
        UpdateWrapper updateWrapper = Wrappers.update();
        updateWrapper.set("update_time", LocalDateTime.now());
        updateWrapper.set("task_status", 3);
        updateWrapper.set("desc", cancelTaskVO.getDesc());
        updateWrapper.eq("task_id", cancelTaskVO.getTaskId());
        boolean update = this.update(updateWrapper);
        return update;
    }

    @Override
    public boolean updateStatus(UpdateStatusVO updateStatusVO) {
        //修改工单状态
        UpdateWrapper updateWrapper = Wrappers.update();
        updateWrapper.set("update_time", LocalDateTime.now());
        updateWrapper.set("task_status", updateStatusVO.getTaskStatus());
        updateWrapper.eq("task_id", updateStatusVO.getTaskId());
        boolean update = this.update(updateWrapper);
        return update;
    }
}
